package com.liu.nb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liu.nb.entity.Debugtask;
import com.liu.nb.entity.DebugtaskPole;
import com.liu.nb.entity.Receiptdebug;
import com.liu.nb.entity.Receiptpatrol;
import com.liu.nb.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 消缺任务表 前端控制器
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/debugtask")
public class DebugtaskController extends BaseController {

    /**
     * 消缺任务查询
     *
     * @param debugtask
     * @param cid
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/getTask2")
    public Result getTask2(Debugtask debugtask, String cid, Integer current, Integer size) {
        IPage<Debugtask> taskList = debugtaskService.getTask2(debugtask, cid, current, size);
        return Result.success(taskList);
    }

    /**
     * 制定消缺任务选择缺陷列表
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getReceiptAll")
    public Result getReceiptAll(Integer current, Integer size) {
        IPage<Receiptpatrol> receiptAll = receiptpatrolService.getReceiptAll(null, null, current, size);
        return Result.success(receiptAll);
    }

    /**
     * 制定消缺任务
     *
     * @param debugtask
     * @param principal
     * @return
     */
    @Transactional
    @PreAuthorize("hasRole('xianluadmin')")
    @RequestMapping("/saveDeBugTask")
    public Result saveDeBugTask(@RequestBody Debugtask debugtask, List<DebugtaskPole> list, Principal principal) {
        debugtask.setCreateuser((iUserService.getByUsername(principal.getName())).getId());
        debugtask.setTime(LocalDateTime.now());
        debugtask.setCancel(1);
        debugtask.setState(0);
        Integer count = debugtaskService.saveDeBugTask(debugtask);
        list.forEach(i ->{
            i.setDebugId(debugtask.getId());
        });
        if (count > 0) {
            boolean b = debugtaskPoleService.updateBatchById(list);
            if(b){
                return Result.success(true);
            }
        } else {
            return Result.success(false);
        }
        return Result.success(false);
    }

    /**
     * 修改消缺任务
     * @param debugtask
     * @param list
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @Transactional
    @RequestMapping("/updateDeBugTask")
    public Result updateDeBugTask(@RequestBody Debugtask debugtask, List<DebugtaskPole> list){
        String id = debugtask.getId();
        debugtask.setCancel(1);
        debugtask.setState(0);
        Integer count = debugtaskService.updateDeBugTask(debugtask);
        list.forEach(i ->{
            i.setDebugId(debugtask.getId());
        });
        if (count > 0) {
            boolean b = debugtaskPoleService.updateBatchById(list);
            if(b){
                return Result.success(true);
            }
        } else {
            return Result.success(false);
        }
        return Result.success(false);
    }

    /**
     * 管理员点击开始消缺任务
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @GetMapping("/implementTask")
    public Result implementTask(String id){
        Debugtask debugtask = new Debugtask();
        debugtask.setId(id);
        debugtask.setState(2);
        boolean b = debugtaskService.updateById(debugtask);
        if(b){
            return Result.success(true);
        }else {
            return Result.success(false);
        }
    }

    /**
     * 消缺员点击完成任务
     * @param id
     * @param description
     * @return
     */
    @PreAuthorize("hasRole('xiaoqueyuan')")
    @GetMapping("endTask")
    public Result endTask(String id, String description){
        Debugtask debugtask = new Debugtask();
        debugtask.setId(id);
        debugtask.setState(3);
        debugtask.setEndtime(LocalDate.now());
        boolean b = debugtaskService.updateById(debugtask);

        if(b){
            Receiptdebug receiptdebug = new Receiptdebug();
            receiptdebug.setDid(id);
            receiptdebug.setDescription(description);
            boolean save = receiptdebugService.save(receiptdebug);
            if(save){
                return Result.success(true);
            }else {
                return Result.success(false);
            }
        }
        return Result.success(false);
    }

//    @RequestMapping("/getOneTesk")
//    public Result getOneTesk(){
//
//    }

}
